package com.example.aixiangshou.runninggame;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.zhouwei.library.CustomPopWindow;
import com.x_memteor.base_master.application.MyApplication;
import com.x_memteor.base_master.base.BaseActivity;
import com.x_memteor.base_master.bean.ItemBean;
import com.x_memteor.base_master.utils.RecycleViewUtils;
import com.x_memteor.base_master.utils.SharedPreferencesUtils;
import com.x_memteor.base_master.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class AddOptionActivity extends BaseActivity {

    @BindView(R.id.spinner)
    TextView spinner;
    @BindView(R.id.recy_option)
    RecyclerView recyOption;
    @BindView(R.id.imv_add)
    ImageView imvAdd;
    @BindView(R.id.imv_subtract)
    ImageView imvSubtract;

    static RecyAdapter recyAdapter;
    static List<ItemBean> list;

    List<IconBean> iconBeanList;
    IconAdapter iconAdapter;

    RecyclerView popRecy;

    static IconBean chooseIconBean;
    static int chooseItem;
    @BindView(R.id.btn_back)
    Button btnBack;
    @BindView(R.id.btn_next)
    Button btnNext;

    EditText changeName;
    Button btnOk;

    public static PopuItemListener itemListener = new PopuItemListener() {
        @Override
        public void onItemClick(IconBean iconBean) {
            chooseIconBean = iconBean;

            if (list.get(chooseItem) != null) {
                list.get(chooseItem).setIcon(chooseIconBean.getIconRec());
            }

            recyAdapter.notifyDataSetChanged();
        }
    };
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.relay_option)
    RelativeLayout relayOption;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initRootView() {
        setContentView(R.layout.activity_add_option);
    }

    @Override
    public void initView() {
        iconBeanList = new ArrayList<>();
        iconBeanList.add(new IconBean(R.drawable.icon_01));
        iconBeanList.add(new IconBean(R.drawable.icon_02));
        iconBeanList.add(new IconBean(R.drawable.icon_03));
        iconBeanList.add(new IconBean(R.drawable.icon_04));
        iconBeanList.add(new IconBean(R.drawable.icon_05));
        iconBeanList.add(new IconBean(R.drawable.icon_06));
        iconBeanList.add(new IconBean(R.drawable.icon_07));
        iconBeanList.add(new IconBean(R.drawable.icon_08));
        iconBeanList.add(new IconBean(R.drawable.icon_09));
        iconBeanList.add(new IconBean(R.drawable.icon_10));
        iconBeanList.add(new IconBean(R.drawable.icon_11));
        iconBeanList.add(new IconBean(R.drawable.icon_12));
        iconBeanList.add(new IconBean(R.drawable.icon_13));
        iconBeanList.add(new IconBean(R.drawable.icon_14));
        iconBeanList.add(new IconBean(R.drawable.icon_15));
        iconBeanList.add(new IconBean(R.drawable.icon_16));
        iconBeanList.add(new IconBean(R.drawable.icon_17));
        iconBeanList.add(new IconBean(R.drawable.icon_18));
        iconBeanList.add(new IconBean(R.drawable.icon_19));
        iconBeanList.add(new IconBean(R.drawable.icon_20));
        iconBeanList.add(new IconBean(R.drawable.icon_21));
        iconBeanList.add(new IconBean(R.drawable.icon_22));
        iconBeanList.add(new IconBean(R.drawable.icon_23));
        iconBeanList.add(new IconBean(R.drawable.icon_24));
        iconBeanList.add(new IconBean(R.drawable.icon_25));
        iconBeanList.add(new IconBean(R.drawable.icon_26));
        iconBeanList.add(new IconBean(R.drawable.icon_27));
        iconBeanList.add(new IconBean(R.drawable.icon_28));
        iconBeanList.add(new IconBean(R.drawable.icon_29));
        iconBeanList.add(new IconBean(R.drawable.icon_30));
        iconBeanList.add(new IconBean(R.drawable.icon_31));

        iconAdapter = new IconAdapter(R.layout.item_icon, iconBeanList);

    }

    CustomPopWindow namePopWindow;

    @Override
    public void initData() {

//        //数据
//        List<String> data_list = new ArrayList<String>();
//        data_list.add("GAME 1");
//        data_list.add("GAME 2");
//        data_list.add("SUGGESTIONS");
//
//        //适配器
//        simpleArrayAdapter arrAdapter = new simpleArrayAdapter<String>(this, R.layout.spanner_item, data_list);
//
//        //设置样式
//        arrAdapter.setDropDownViewResource(R.layout.spanner_item);
//
//        //加载适配器
//        spinner.setAdapter(arrAdapter);
//        spinner.setSelection(data_list.size() - 1, true);

        list = new ArrayList<>();

        RecycleViewUtils.getVerticalLayoutManager(AddOptionActivity.this, recyOption);
        recyAdapter = new RecyAdapter(R.layout.item_recy, list);
        recyOption.setAdapter(recyAdapter);

        recyAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, final int position) {

                switch (view.getId()) {
                    case R.id.tv_item:
                        View contentView2 = LayoutInflater.from(AddOptionActivity.this).inflate(R.layout.pop_set_name, null);
                        changeName = contentView2.findViewById(R.id.edit_name);
                        btnOk = contentView2.findViewById(R.id.btn_ok);
                        btnOk.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                list.get(position).setName(changeName.getText().toString() + "");
                                if (namePopWindow != null) {
                                    namePopWindow.dissmiss();
                                }
                                recyAdapter.notifyDataSetChanged();
                            }
                        });

                        namePopWindow = new CustomPopWindow.PopupWindowBuilder(AddOptionActivity.this)
                                .setView(contentView2)//显示的布局
                                .enableBackgroundDark(true) //弹出popWindow时，背景是否变暗
                                .setBgDarkAlpha(0.7f) // 控制亮度
                                .create()//创建PopupWindow
                                .showAtLocation(recyOption.getRootView(), Gravity.CENTER, 0, 0);

                        break;
                    case R.id.imv_item:
//                        View contentView = LayoutInflater.from(AddOptionActivity.this).inflate(R.layout.pop_lay, null);
//                        popRecy = contentView.findViewById(R.id.recy_icon);
//                        RecycleViewUtils.getGridLayoutManager(AddOptionActivity.this, popRecy);
//                        popRecy.setAdapter(iconAdapter);
//
                        chooseItem = position;
//
//                        final CustomPopWindow popWindow = new CustomPopWindow.PopupWindowBuilder(AddOptionActivity.this)
//                                .setView(contentView)//显示的布局
//                                .enableBackgroundDark(true) //弹出popWindow时，背景是否变暗
//                                .setBgDarkAlpha(0.7f) // 控制亮度
//                                .create()//创建PopupWindow
//                                .showAtLocation(recyOption.getRootView(), Gravity.CENTER, 0, 0);

                        Intent intent = new Intent(AddOptionActivity.this, PopuActivity.class);
                        startActivity(intent);

//                        iconAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
//                            @Override
//                            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
//                                chooseIconBean = (IconBean) adapter.getData().get(position);
//
//                                if (list.get(chooseItem) != null) {
//                                    list.get(chooseItem).setIcon(chooseIconBean.getIconRec());
//                                }
//
//                                recyAdapter.notifyDataSetChanged();
//                            }
//                        });
                        break;
                }

            }
        });
    }

    @Override
    public void initListener() {

    }

    @OnClick({R.id.imv_add, R.id.imv_subtract, R.id.btn_back, R.id.btn_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.imv_add:
                if (list.size() > 16) {
                    ToastUtils.showToast(AddOptionActivity.this, "You can add up to 16 ");
                } else {
                    list.add(new ItemBean("OPTION " + (list.size() + 1)));
                    recyAdapter.notifyDataSetChanged();
                }
                break;
            case R.id.imv_subtract:
                if (list.size() > 0) {
                    list.remove(list.size() - 1);
                    recyAdapter.notifyDataSetChanged();
                }
                break;
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_next:
                MyApplication.getInstance().getGameBean().setList(list);
                MyApplication.getInstance().saveGameBean(spinner.getText() + "");
                startActivity(new Intent(AddOptionActivity.this, ChooseOptionActivity.class));
                break;
        }
    }

    public static PopuItemListener getItemListener() {
        return itemListener;
    }

    CustomPopWindow customPopWindow;

    @OnClick(R.id.spinner)
    public void onViewClicked() {
        showClosePow();
    }

    EditText game1, game2;
    boolean game1True, game2True;


    private void showClosePow() {
        View contentView = LayoutInflater.from(AddOptionActivity.this).inflate(R.layout.pop_add_option, null);
        handleClose(contentView);

        game1 = contentView.findViewById(R.id.edit_game1);
        game2 = contentView.findViewById(R.id.edit_game2);

        game1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    game1True = true;
                } else {
                    game1True = false;
                }
            }
        });

        game2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    game2True = true;
                } else {
                    game2True = false;
                }
            }
        });
        game1.setText(SharedPreferencesUtils.getString(AddOptionActivity.this, "game1", "GAME 1"));
        game2.setText(SharedPreferencesUtils.getString(AddOptionActivity.this, "game2", "GAME 2"));

        customPopWindow = new CustomPopWindow.PopupWindowBuilder(AddOptionActivity.this)
                .setView(contentView)//显示的布局
                .enableBackgroundDark(true) //弹出popWindow时，背景是否变暗
                .setBgDarkAlpha(0.7f) // 控制亮度
                .create()//创建PopupWindow
                .showAsDropDown(spinner, 0, 0);
    }


    /**
     * 处理弹出显示内容、点击事件等逻辑
     *
     * @param contentView
     */
    private void handleClose(View contentView) {
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.tv_ok:
                        if (game1 != null) {
                            SharedPreferencesUtils.putString(AddOptionActivity.this, "game1", game1.getText().toString() + "");
                            if (game1True) {
                                spinner.setText(game1.getText().toString() + "");
                            }
                        }
                        if (game2 != null) {
                            SharedPreferencesUtils.putString(AddOptionActivity.this, "game2", game2.getText().toString() + "");
                            if (game2True) {
                                spinner.setText(game2.getText().toString() + "");
                            }
                        }
                        if (customPopWindow != null) {
                            customPopWindow.dissmiss();
                        }
                         if (MyApplication.getInstance().getGameBean(spinner.getText() + "") != null){
                             list = MyApplication.getInstance().getGameBean().getList();
                             if (list != null){
                                 recyAdapter.setNewData(list);
                             }else {
                                 list = new ArrayList<>();
                                 recyAdapter.setNewData(list);
                             }
                         }else {
                             list = new ArrayList<>();
                             recyAdapter.setNewData(list);
                         }
                        break;
                }
            }
        };
        contentView.findViewById(R.id.tv_ok).setOnClickListener(listener);

    }
}
